Thread: make: *** [transform] Error 1

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    11

    make: *** [transform] Error 1

    Please help ive been working on this for two weeks and I'm having this error and every time I try to run it to print the new image I get a seg fault I commented out my mirror() function so I know its not causing it. Its something in my main or readImage() function. This is the error I get if I compile without make:

    [17:53:59] cehutto@hornet19:~/asg21 [39] gcc transform.h transform.c parset.c print.c
    /tmp/ccoTPJTA.o: In function `main':
    transform.c.text+0x8f): undefined reference to `mirror'
    collect2: ld returned 1 exit status
    Header File:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define CREATOR "Cody Hutto"
    #define RGB_COMPONENT_COLOR 255
    
    
    typedef struct PPMPixel {
    
        unsigned char r, g, b;
    
    }PPMPixel;
    
    typedef struct PPMImage {
    
        int x, y;
        PPMPixel *data;
        int g_width, g_height;
    
    }PPMImage;
    
    
    
    PPMImage *readPPM(const char *filename);
    
    
    // other function prototypes here
    void writePPM(const char *filename, PPMImage *img);
    void mirror(PPMImage *img);
    This is my main:

    Code:
    #include "transform.h"
    #include <string.h>
    
    int main (int argc, char *argv[])  {
    
        FILE *inFile;
        int c;
            
    
        //open input file
        inFile = fopen("tiger.ppm", "r");
        if (inFile == NULL) {
            fprintf(stderr,"Output failure. Exiting program");
            exit(0);
        }
    
    
        PPMImage *image;
        image = readPPM("tiger.ppm");
    
        c = atoi(argv[1]);
    
        
        if(c == 1){
            mirror(image);
            writePPM("tiger1.ppm", image); 
        }
        else if(c == 2){
    
            //flipImage(image);
            writePPM("tiger2.ppm", image);
        }
        else if(c == 3){
            
            //rotate(image);
            writePPM("tiger3.ppm", image);
        }
    
    
    
        return (1);
    }
    This is a parser for the ppm:

    Code:
    #include "transform.h"
    #include <errno.h>
    #include <string.h>
    
    
    
    PPMImage *readPPM(const char *filename) {
        
        char buff[16];
        PPMImage *img;
        FILE *fp;
        char *resultstr;
        int c, rgb_comp_color;
        
        //open PPM file for reading
        fp = fopen(filename, "rb");
        if (!fp) {
            fprintf(stderr, "Unable to open file '%s'\n", filename);
            exit(1);
        }
        
             //read image format
             if (!(resultstr = fgets(buff, sizeof(buff), fp))) {
            fprintf(stderr, "echo %s\n", buff);              
            //perror(strerror(errno));
                      fprintf(stderr, "error(%d): %s\n", errno, strerror(errno));
            exit(1);
             }
        
        //check the image format
        if (buff[0] != 'P' || buff[1] != '6') {
            fprintf(stderr, "Invalid image format (must be 'P6')\n");
            exit(1);
        }
        
        //alloc memory form image
        img = (PPMImage *)malloc(sizeof(PPMImage));
        if (!img) {
        fprintf(stderr, "Unable to allocate memory\n");
        exit(1);
        }
        
        //check for comments
        c = getc(fp);
        while (c == '#') {
        while (getc(fp) != '\n') ;
        c = getc(fp);
        }
    
        ungetc(c, fp);
        //read image size information
        if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
            fprintf(stderr, "Invalid image size (error loading '%s')\n",             filename);
                 exit(1);
        }
    
        //read rgb component
        if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
            fprintf(stderr, "Invalid rgb component (error loading '%s')\n",         filename);
            exit(1);
        }
        
        //check rgb component depth
        if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
            fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
            exit(1);
        }
    
        while (fgetc(fp) != '\n') ;
        //memory allocation for pixel data
        img->data = (PPMPixel*)malloc(img->x* img->y * sizeof(PPMPixel));
        
        if (!img) {
            fprintf(stderr, "Unable to allocate memory\n");
            exit(1);
        }
    
        //read pixel data from file
        if (fread(img->data, 3 * img->x, img->y, fp) != img->y)         {
            fprintf(stderr, "Error loading image '%s'\n", filename);
            exit(1);
        }
    
        fclose(fp);
        return img;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > gcc transform.h transform.c parset.c print.c
    You don't pass .h files to the compiler.

    You've got this
    void mirror(PPMImage *img);

    What you're missing is the implementation - it's the thing that actually DOES the work.
    Code:
    void mirror(PPMImage *img) {
      // your code here
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    I'm most certainly not missing it. However I got rid of the error but now I get a seg fault

    Code:
    #include "transform.h" 
    #include <stdio.h> 
     
    void mirror(PPMImage *img) { 
     
        int y, x; 
        const int middleX = img->x/2; 
        PPMImage tmp; 
        fprintf(stderr, "%d %d\n", img->x, img->y); 
        for(y=0; y<img->y; y++){ 
     
            //img = img->data + y * img->x; 
            for(x=0; x<middleX; x++) { 
     
                tmp = img[x + (img->x * y)]; 
                img[x + (img->x * y)] = img[img->x - 1 - x + (img->x * y)]; 
                img[img->x - 1 - x + (img->x * y)] = tmp; 
            } 
        } 
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So what do you type to run the program?

    It needs a command line parameter, like say
    ./a.out 1

    Now is a good time to learn about gdb.
    At the very least, you'll discover which line of code the segfault is on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    Quote Originally Posted by Salem View Post
    > gcc transform.h transform.c parset.c print.c
    You don't pass .h files to the compiler.

    You've got this
    void mirror(PPMImage *img);

    What you're missing is the implementation - it's the thing that actually DOES the work.
    Code:
    void mirror(PPMImage *img) {
      // your code here
    }
    This is the full code and my new seg fault:


    Code:
    #include<stdio.h> 
    #include<stdlib.h> 
     
    typedef struct { 
         unsigned char r,g,b; 
    } PPMPixel; 
     
    typedef struct { 
         int x, y; 
         PPMPixel *data; 
    } PPMImage; 
     
    #define CREATOR "Cody Hutto" 
    #define RGB_COMPONENT_COLOR 255 
     
    static PPMImage *readPPM(const char *filename) 
    { 
             char buff[100]; 
             PPMImage *img; 
             FILE *fp; 
             int c, rgb_comp_color; 
             //open PPM file for reading 
             fp = fopen(filename, "rb"); 
             if (!fp) { 
                  fprintf(stderr, "Unable to open file '%s'\n", filename); 
                  exit(1); 
             } 
     
             //read image format 
             if (!fgets(buff, sizeof(buff), fp)) { 
                  perror(filename); 
                  exit(1); 
             } 
     
        //check the image format 
        if (buff[0] != 'P' || buff[1] != '6') { 
             fprintf(stderr, "Invalid image format (must be 'P6')\n"); 
             exit(1); 
        } 
     
        //alloc memory form image 
        img = (PPMImage *)malloc(sizeof(PPMImage)); 
        if (!img) { 
             fprintf(stderr, "Unable to allocate memory\n"); 
             exit(1); 
        } 
     
        //check for comments 
        c = getc(fp); 
        while (c == '#') { 
        while (getc(fp) != '\n') ; 
             c = getc(fp); 
        } 
     
        ungetc(c, fp); 
        //read image size information 
        if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) { 
             fprintf(stderr, "Invalid image size (error loading '%s')\n", filename); 
             exit(1); 
        } 
     
        //read rgb component 
        if (fscanf(fp, "%d", &rgb_comp_color) != 1) { 
             fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename); 
             exit(1); 
        } 
     
        //check rgb component depth 
        if (rgb_comp_color!= RGB_COMPONENT_COLOR) { 
             fprintf(stderr, "'%s' does not have 8-bits components\n", filename); 
             exit(1); 
        } 
     
        while (fgetc(fp) != '\n') ; 
        //memory allocation for pixel data 
        img->data = (PPMPixel*)malloc(img->x * img->y * sizeof(PPMPixel)); 
     
        if (!img) { 
             fprintf(stderr, "Unable to allocate memory\n"); 
             exit(1); 
        } 
     
        //read pixel data from file 
        if (fread(img->data, 3 * img->x, img->y, fp) != img->y) { 
             fprintf(stderr, "Error loading image '%s'\n", filename); 
             exit(1); 
        } 
     
        fclose(fp); 
        return img; 
    } 
    void writePPM(const char *filename, PPMImage *img) 
    { 
        FILE *fp; 
        //open file for output 
        fp = fopen(filename, "wb"); 
        if (!fp) { 
             fprintf(stderr, "Unable to open file '%s'\n", filename); 
             exit(1); 
        } 
     
        //write the header file 
        //image format 
        fprintf(fp, "P6\n"); 
     
        //comments 
        fprintf(fp, "# Created by %s\n",CREATOR); 
     
        //image size 
        fprintf(fp, "%d %d\n",img->x,img->y); 
     
        // rgb component depth 
        fprintf(fp, "%d\n",RGB_COMPONENT_COLOR); 
     
        // pixel data 
        fwrite(img->data, 3 * img->x, img->y, fp); 
        fclose(fp); 
    } 
     
    void mirror(PPMImage *img) { 
     
        int y, x; 
        const int middleX = img->x/2; 
        PPMImage tmp; 
        fprintf(stderr, "%d %d\n", img->x, img->y); 
        for(y=0; y<img->y; y++){ 
     
            //img = img->data + y * img->x; 
            for(x=0; x<middleX; x++) { 
     
                tmp = img[x + (img->x * y)]; 
                img[x + (img->x * y)] = img[img->x - 1 - x + (img->x * y)]; 
                img[img->x - 1 - x + (img->x * y)] = tmp; 
            } 
        } 
    } 
     
     
    int main(){ 
        PPMImage *image; 
        image = readPPM("tiger.ppm"); 
        mirror(image); 
        writePPM("tiger2.ppm",image); 
        printf("Press any key..."); 
        getchar(); 
    }
    Seg Fault;

    [18:43:38] cehutto@hornet19:~/101/lab12 [162] ./a.out
    690 461
    Segmentation fault

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Like I said - learn some tools.

    $ gcc -g transform.c parset.c print.c
    The -g flag adds debug information to the program

    $ gdb -q ./a.out
    Load the program into gdb

    (gdb) run
    Run the program

    690 461
    Segmentation fault

    (gdb) bt
    Display a stack trace from the point of the crash all the way back to main.

    The next useful commands are 'up' 'down' and 'print'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    I have done this but still cannot figure out the error. Although it points here
    img[img->x - 1 - x + (img->x * y)] = tmp;
    Line 133

  8. #8
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    Quote Originally Posted by Salem View Post
    Like I said - learn some tools.

    $ gcc -g transform.c parset.c print.c
    The -g flag adds debug information to the program

    $ gdb -q ./a.out
    Load the program into gdb

    (gdb) run
    Run the program

    690 461
    Segmentation fault

    (gdb) bt
    Display a stack trace from the point of the crash all the way back to main.

    The next useful commands are 'up' 'down' and 'print'
    My math is correct even my teacher said so.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    is img an array ?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So do these things now
    print x
    print y
    print img->x
    print img->y
    print img->x - 1 - x + (img->x * y)

    Do those values look OK to you?

    Because the machine clearly thinks the last one is so far outside the bounds of the memory you allocated that it just terminated the program.
    That is what the segfault is - a bad memory access.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    I get the x to loop till the mid point but y will not go past seven please help me I have been at this for a week with nothing to show.

    Code:
    void mirror(PPMImage *img) {
    
        int y, x;
        const int middleX = img->x/2;
        PPMImage tmp;
        
        for(y=0; y<img->y; y++){
        
            //img = img->data + y * img->x;
            for(x=0; x<middleX; x++) {
                         
                tmp = img[x + (img->x * y)];
    
            img[x + (img->x * y)] = img[img->x  -  x + (img->x * y)];
                //fprintf(stderr, "y = %d\n ", tmp[y]);
                img[img->x  -  x + (img->x * y)] = tmp;
                fprintf(stderr, "img->x = %d\n", img->x);
                fprintf(stderr, "img->y = %d\n", img->y);
                fprintf(stderr, "middle= %d\n", middleX);
                fprintf(stderr, "y = %d\n ", y);
                fprintf(stderr, "x = %d\n ", x);
            }
        }
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    And the indexes - why not output these as well?
    Code:
                tmp = img[x + (img->x * y)];
     
            img[x + (img->x * y)] = img[img->x  -  x + (img->x * y)];
                //fprintf(stderr, "y = %d\n ", tmp[y]);
                img[img->x  -  x + (img->x * y)] = tmp;
    How about simplify?
    Code:
      int a = x + (img->x * y);
      int b = img->x  -  x + (img->x * y);
      tmp = img[a];
      img[a] = img[b];
      img[b] = tmp;
    Output a and b in your printfs as well, and CHECK that they're in bounds.

    Or even add your own checks
    Code:
    if ( a < 0 || a >= whatever ) // OOPS!!!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Transform help
    By fadlan12 in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2010, 07:32 PM
  2. trouble with transform
    By curlious in forum C++ Programming
    Replies: 6
    Last Post: 06-11-2006, 04:00 PM
  3. transform help!!!
    By what3v3r in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 10:27 PM
  4. Fourier Transform
    By srikanthreddy in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2005, 08:36 AM
  5. transform int to char
    By WrathFox in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2004, 02:27 AM